package top.huiwow.entity;

import com.c45y.Bastille.BastilleEntity;
import net.minecraft.server.v1_8_R3.Entity;
import net.minecraft.server.v1_8_R3.EntityLiving;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Listener;
import org.bukkit.metadata.FixedMetadataValue;
import top.huiwow.GameCore;
import top.huiwow.listeners.EntityListener;
import top.huiwow.utils.MathUtils;

import java.lang.reflect.InvocationTargetException;

public abstract class AkaishiEntity implements Listener {

    abstract int getInternalID();

    abstract void setInternalID(int i);

    abstract Class<? extends BastilleEntity> getEntityType();

    public void spawn(Location location, int level) {
        getTheEntity(location,level).spawn(location);
    }
    public BastilleEntity getTheEntity(Location location,int level){

        BastilleEntity basicEntity;
        try {
            basicEntity = getEntityType().getConstructor(World.class).newInstance(location.getWorld());
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        EntityStats stats = getStats(level);
        GameCore plugin = GameCore.getInstance();
        LivingEntity ent = (LivingEntity) ((Entity) basicEntity).getBukkitEntity();
        // setInternalID(plugin.getEntityManager().getInternalID(this.getClass()));
        ent.setMetadata("internalID", new FixedMetadataValue(plugin, getInternalID()));
        ent.setMetadata("max_health", new FixedMetadataValue(plugin, stats.maxHealth));
        ent.setMetadata("level", new FixedMetadataValue(plugin, level));
        ent.setMetadata("name", new FixedMetadataValue(plugin, stats.name));
        ent.setMetadata("nokb", new FixedMetadataValue(plugin, stats.nokb));
        ent.setMaxHealth(stats.maxHealth);
        ent.setHealth(stats.maxHealth);
        ent.setCustomName(ChatColor.GRAY + "LV " + level + ChatColor.RESET + "   " + ChatColor.RED + ChatColor.BOLD + stats.name + "  " + ChatColor.RESET + " " + ChatColor.GREEN + MathUtils.simplifyBigNumber(stats.maxHealth, true) + ChatColor.RESET + "/" + ChatColor.GREEN + MathUtils.simplifyBigNumber(stats.maxHealth, true) + ChatColor.RED + ChatColor.RED + ChatColor.BOLD + "❤");
        ent.setCustomNameVisible(true);
        if (basicEntity instanceof EntityLiving) {
            ((EntityLiving) basicEntity).maxNoDamageTicks = 0;
            ((EntityLiving) basicEntity).noDamageTicks = 0;
        }
        EntityListener.dropMap.put(getInternalID(), stats.drops);

        return basicEntity;
    }

    abstract EntityStats getStats(int level);
}
